package com.Blog.www.servlet;

import com.Blog.www.constant.DataConstant;
import com.Blog.www.constant.PageConstant;
import com.Blog.www.po.User;
import com.Blog.www.service.BlogService;
import com.Blog.www.service.Impl.BlogServiceImpl;
import com.Blog.www.service.Impl.LikeServiceImpl;
import com.Blog.www.service.Impl.UserServiceImpl;
import com.Blog.www.service.LikeService;
import com.Blog.www.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * @author 君未洋
 */
@WebServlet("/UserLike/*")
public class LikeServlet extends BaseServlet {

    private static UserService userService = new UserServiceImpl();
    private static BlogService blogService = new BlogServiceImpl();
    private static LikeService likeService = new LikeServiceImpl();

    /**
     * 获取用户收藏相关的方法
     */
    public void userLike(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        String uid = req.getParameter("uid");
        String bid = req.getParameter("bid");
        //先获取下上传的id
        String status = req.getParameter("status");

        //这个是获取要进行的操作

        req.removeAttribute(PageConstant.BLOG_I);
        //出于安全考虑,还是先校验一下,如果不对直接跳转回原来的博客页面
        likeService.userLike(uid, bid, status, req, resp);
    }


    /**
     * 展示用户收藏用
     */
    public void myLike(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");

        //获取下现在登录着的用户
        User user = (User) req.getSession().getAttribute("user");

        //调用方法查询用户的收藏夹,返回文章id列表
        List<Integer> bids = likeService.findUserLike(user.getId());
        //将文章的id列表上传到req,然后转发到BlogServlet中处理展示

        req.removeAttribute(DataConstant.BLOG_M);
        req.setAttribute(DataConstant.BLOG_M, bids);

        req.getRequestDispatcher("/UserBlog/BlogLikeList").forward(req, resp);
    }

}
